package com.sakura.eduservice.controller;


import com.sakura.commonutils.R;
import com.sakura.eduservice.client.VodClient;
import com.sakura.eduservice.entity.EduVideo;
import com.sakura.eduservice.service.EduVideoService;
import com.sakura.servicebase.exceptionhandler.GuliException;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-03-02
 */
@RestController
@Slf4j
@RequestMapping("/eduservice/video")
public class EduVideoController {
    @Autowired
    private EduVideoService videoService;
    @Autowired
    private VodClient vodClient;

    @ApiOperation("添加小节")
    @PostMapping("addVideo")
    public R addVideo(@RequestBody EduVideo eduVideo){
        videoService.save(eduVideo);
        return R.ok();
    }


    @ApiOperation("删除小节")
    @DeleteMapping("{id}")
    public R deleteVideo(@PathVariable String id){
        log.info("远程调用该deleteVideo方法");
        //根据小节id获取视频id,调用方法实现视频删除
        EduVideo eduVideo = videoService.getById(id);
        String videoSourceId = eduVideo.getVideoSourceId();

        if (null!=videoSourceId&&!"".equals(videoSourceId)){
            //根据视频id,远程调用实现视频删除
            R result = vodClient.removeAlyVideo(videoSourceId);
            if (result.getCode()==20001){
                return result;
//                throw new GuliException(20001,"删除视频失败,熔断器...");
            }
        }
        videoService.removeById(id);
        return R.ok();
    }



    //修改小节 TODO
}

